package com.wl.center.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.center.model.task.Customer;
import com.wl.center.task.service.CustomerInfoService;
import com.wl.center.task.service.CustomerService;
import com.wl.center.vo.task.CustomerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {
    @Autowired
    CustomerService customerService;
    @Override
    public IPage<CustomerInfo> getCustomerInfoPage(Page<Customer> page, QueryWrapper<Customer> wrapper) {
        List<CustomerInfo> customerInfoList = new ArrayList<>();//引入中间类CustomerInfo
        CustomerInfo customerInfo = new CustomerInfo();//创建一个中间类对象CustomerInfo
        //调用方法实现分页查询
        IPage<Customer> pageCustomer = customerService.page(page, wrapper);//通过查customer表把一些基本信息取出
        List<Customer> customerList = pageCustomer.getRecords();//将这些基本信息放到customerList
        IPage<CustomerInfo> page1 = new Page<>();
        for (Customer customer:customerList) {
            Integer id = customer.getId();
            String name = customer.getName();
            String phone = customer.getPhone();
            String city = customer.getCity();
            customerInfo= new CustomerInfo();
            customerInfo.setId(id);
            customerInfo.setCity(city);
            customerInfo.setName(name);
            customerInfo.setPhone(phone);
            customerInfoList.add(customerInfo);
        }
        long pages = pageCustomer.getPages();
        long current = pageCustomer.getCurrent();
        long size = pageCustomer.getSize();
        long total = pageCustomer.getTotal();
        page1.setPages(pages);
        page1.setCurrent(current);
        page1.setSize(size);
        page1.setTotal(total);
        page1.setRecords(customerInfoList);

        return page1;
    }

    @Override
    public List<CustomerInfo> getCustomerInfo(QueryWrapper<Customer> wrapper) {
        List<CustomerInfo> customerInfoList = new ArrayList<>();//引入中间类CustomerInfo
        CustomerInfo customerInfo = new CustomerInfo();//创建一个中间类对象CustomerInfo
        List<Customer> customerList = customerService.list(wrapper);//通过查customer表把一些基本信息取出
        for (Customer customer:customerList) {
            Integer id = customer.getId();
            String name = customer.getName();
            String phone = customer.getPhone();
            String city = customer.getCity();
            customerInfo= new CustomerInfo();
            customerInfo.setId(id);
            customerInfo.setCity(city);
            customerInfo.setName(name);
            customerInfo.setPhone(phone);
            customerInfoList.add(customerInfo);
        }
        return customerInfoList;
    }
}
